--
-- Author: gnar
-- Date: 2014-12-05 15:21:48
--

ChatSystem = {}

ChatSystem.maxChatItemNum = 60
ChatSystem.sysChatWidth = 600
ChatSystem.userChatWidth = 300

ChatSystem.sysUid = 0

ENUM_CHAT_TYPE = {
    total = 0,
    world = 1,
    union = 2,
    team = 3,
    person = 4,   
}

ENUM_CHAT_EVENT_NAME = {
    NEW_CHAT = "NEW_CHAT",
}

function ChatSystem:init()  

    SocketProxy:addCMDListener(SocketCommand.CHAT_OL, handler(ChatSystem,ChatSystem._onReceiveChat), false, false, 0)

    ChatSystem.totalChatList = {}
    ChatSystem.worldChatList = {}
    ChatSystem.unionChatList = {}
    ChatSystem.teamChatList = {}
    ChatSystem.personChatList = {}

    ChatSystem.totalChatHeightList = {}
    ChatSystem.worldChatHeightList = {}
    ChatSystem.unionChatHeightList = {}
    ChatSystem.teamChatHeightList = {}
    ChatSystem.personChatHeightList = {}

    ChatSystem.latestSystemChatItem = {}
    ChatSystem.latestSystemChatItem.msg = WordManager:getWord("ChatLayer_012")
    ChatSystem.latestSystemChatItem.src_uid = ChatSystem.sysUid
    ChatSystem.latestSystemChatItem.src_nick = WordManager:getWord("ChatLayer_006")

    --ChatSystem.chatListInsertCallBack = nil

end

function ChatSystem:getChatHeight(chatItem)  -- 

    local text = chatItem.msg
    local fontProps = {size = 25, font = "Helvetica-Bold"}
    fontProps.text = text
    if ChatSystem.retainedLabel ==nil then 
        ChatSystem.retainedLabel = ui.newTTFLabel(fontProps)
        ChatSystem.retainedLabel:retain()
    else
        ChatSystem.retainedLabel:setString(fontProps.text)
    end
    local rowNum
    if chatItem.src_uid == ChatSystem.sysUid then 
        rowNum = math.floor(ChatSystem.retainedLabel:getBoundingBox().size.width/ChatSystem.sysChatWidth) +1 
    else
        rowNum = math.floor(ChatSystem.retainedLabel:getBoundingBox().size.width/ChatSystem.userChatWidth) +1 
    end
    local height = rowNum * ChatSystem.retainedLabel:getBoundingBox().size.height
    return height,rowNum

end

function ChatSystem:sendChat(chatItem,handler)  
    
    ChatSystem:_insertChatItem(chatItem, 0)  
    ChatSystem:_insertChatItem(chatItem)  

    local send_buff = ByteArray:create(0);
    send_buff:write_uint32(chatItem.to_uid);
    send_buff:write_uint16(string.len(chatItem.msg));
    send_buff:write_buf(chatItem.msg, string.len(chatItem.msg));
    SocketProxy:send(SocketCommand.CHAT_OL, nil, send_buff, false);

end

function ChatSystem:_onReceiveChat(sdata)  --1、更新 chatData  2、广播    uid==0 时是系统消息 

    if sdata.ret == 0 then
        local chatItem = ChatSystem:parseChatItem(sdata.ba) 
        --print("test 12345",msgItem.src_uid,DataSystem.friendData:getBlackedUidList()[msgItem.src_uid])
        if  DataSystem.friendData:getBlackedUidList()[tonumber(chatItem.src_uid)] then
        else
            if tonumber(chatItem.src_uid) == ChatSystem.sysUid then  
                ChatSystem.latestSystemChatItem.msg = chatItem.msg
            end
            ChatSystem:_insertChatItem(chatItem, 0)  
            ChatSystem:_insertChatItem(chatItem)  

            NotificationManager:post(ENUM_CHAT_EVENT_NAME.NEW_CHAT, chatItem)  
        end
    end

end

function ChatSystem:getChatListByType(chatType)  

    local targetLists = {[0] = "totalChatList", "worldChatList", "unionChatList", "teamChatList", "personChatList"} 
    local targetList = ChatSystem[ targetLists[chatType] ]
    return targetList

end

function ChatSystem:getChatLableHeightLists(chatType)  

    local targetLists = {[0] = "totalChatHeightList", "worldChatHeightList", "unionChatHeightList", "teamChatHeightList", "personChatHeightList"} 
    local targetList = ChatSystem[ targetLists[chatType] ]
    return targetList

end

--to_uid = 0 时表示全部
function ChatSystem:_insertChatItem(chatItem, chatType)  

    local tChatType = chatType or chatItem.chatType

    local targetList = ChatSystem:getChatListByType(tChatType)  
    local targetHeightList = ChatSystem:getChatLableHeightLists(tChatType) 


    if tonumber(chatItem.src_uid) == ChatSystem.sysUid then -- 系统消息
        chatItem.src_nick = WordManager:getWord("ChatLayer_006")
    else
        chatItem.msg  = filterMessage(chatItem.msg)
    end  

    local height ,row= ChatSystem:getChatHeight(chatItem)

    if table.nums(targetList) >= ChatSystem.maxChatItemNum then
        table.remove(targetList, 1)
        table.insert(targetList, chatItem)
        table.remove(targetHeightList, 1)
        table.insert(targetHeightList,{height,row})
    else
        table.insert(targetList, chatItem)
        table.insert(targetHeightList,{height,row})
    end 

    --if ChatSystem.chatListInsertCallBack then ChatSystem.chatListInsertCallBack(tChatType) end
end

function ChatSystem:parseChatItem(sdata,cdata) 

    local chatItem = {}
    if sdata then
        chatItem.to_uid = sdata:read_uint32()
        chatItem.chatType = chatItem.to_uid
        if chatItem.chatType > 1000 then
            chatItem.chatType = 4 
        end
        chatItem.src_uid = sdata:read_uint32()
        chatItem.src_nick = sdata:readStringBytes(sdata:read_uint16());
        chatItem.head_id = sdata:read_uint32()
        chatItem.msg = sdata:readStringBytes(sdata:read_uint16());
    elseif cdata then
        chatItem.to_uid = cdata.to_uid or 1
        chatItem.chatType = chatItem.to_uid
        if chatItem.chatType > 1000 then
            chatItem.chatType = 4 
        end
        chatItem.src_uid = cdata.src_uid or DataSystem.userData:getSelfInfo().uid
        chatItem.src_nick = cdata.src_nick or DataSystem.userData:getSelfInfo().nick
        chatItem.head_id = cdata.head_id or DataSystem.userData.selfInfo.head_id
        chatItem.msg = cdata.msg or ""
    end

    return chatItem

end



